package multimedia.uebung;

import multimedia.uebung.enums.colorSpace;

public class Converter {
	
	public static void convertToYCbCr(Image image)
	{
		int Y = 0;
		int Cb = 0;
		int Cr = 0;
		
		final int OFFSET  = 128;
		
		int [][] rgbLayer_r_ref = image.getRgbLayer_r_ref();
		int [][] rgbLayer_g_ref = image.getRgbLayer_g_ref();
		int [][] rgbLayer_b_ref = image.getRgbLayer_b_ref();
		
		int [][] ycbcrLayer_Y_ref = new int [image.getHeightWithStepsize()][image.getWidthWithStepsize()];
		int [][] ycbcrLayer_Cb_ref = new int [image.getHeightWithStepsize()][image.getWidthWithStepsize()];
		int [][] ycbcrLayer_Cr_ref = new int [image.getHeightWithStepsize()][image.getWidthWithStepsize()];
		
		
		if(image.getColor() == colorSpace.RGB)
		{
			image.setColor(colorSpace.YCbCr);			
			for(int i=0; i < image.getHeightWithStepsize(); i++)
			{
	            for(int j=0; j < image.getWidthWithStepsize(); j++)
	            {

                    Y = (int)(0.299 * rgbLayer_r_ref[i][j] + 0.587 * rgbLayer_g_ref[i][j] + 0.114 * rgbLayer_b_ref[i][j] + 0.5) - OFFSET;				
                   	Cb = (int)((-0.1687) * rgbLayer_r_ref[i][j] + (-0.3312) * rgbLayer_g_ref[i][j] + 0.5 * rgbLayer_b_ref[i][j] + 0.5 + 128.0) - OFFSET;
                    Cr = (int)(0.5 * rgbLayer_r_ref[i][j] + (-0.4186) * rgbLayer_g_ref[i][j] + (-0.0813) * rgbLayer_b_ref[i][j] + 0.5 + 128.0) - OFFSET;

                    
                    ycbcrLayer_Y_ref[i][j] = Y;
                    ycbcrLayer_Cb_ref[i][j] = Cb;
                    ycbcrLayer_Cr_ref[i][j] = Cr;
	                    
	            }//end for j
			}//end for i
			
			image.setYcbcrLayer_Y_ref(ycbcrLayer_Y_ref);
			image.setYcbcrLayer_Cb_ref(ycbcrLayer_Cb_ref);
			image.setYcbcrLayer_Cr_ref(ycbcrLayer_Cr_ref);
		}else
		{
			System.out.println("Farbmodell ist bereits YCbCr");
		}//end if else
		
	}//end method

}
